﻿@using System.Collections.Generic
@using Matrix.MaKaYunShang.BLL
@model Matrix.MaKaYunShang.Model.Shops

@{
    ViewBag.Title = "编辑店铺";
    ViewBag.PathToBtn = "/Shops/List";
    ViewBag.TextBtn = "返回至列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                店铺信息
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("Update", "Shops", FormMethod.Post, new { id = "frmUpdate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-lg-12">
                            <fieldset>
                                @Html.HiddenFor(model => model.Id)
                                @Html.HiddenFor(model => model.WechatParamQrCodeId)
                                @Html.HiddenFor(model => model.Logo)
                                @Html.HiddenFor(model => model.Created)
                                @Html.HiddenFor(model => model.CreatedBy)
                                @Html.HiddenFor(model => model.IsActive)
                                @Html.HiddenFor(model => model.IsDelete)
                                <div class="form-group col-md-6">
                                    <label>店铺名称</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Name, new { @class = "form-control", placeholder = "名称" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>营业状态</label>
                                    <div class="input-group">
                                        @Html.DropDownList("BusinessStatus", (IEnumerable<SelectListItem>)ViewBag.lsBusinessStatus, new { @class = "form-control" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>所属行业</label>
                                    <div class="input-group">
                                        @Html.DropDownList("IndustryId", (IEnumerable<SelectListItem>)ViewBag.ParentIndustryList, new { @class = "form-control" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>细分行业</label>
                                    <div class="input-group">
                                        <select id="SeparatedIndustryId" name="SeparatedIndustryId" class="form-control"></select>
                                        <input type="hidden" id="hidSeparatedIndustryId" value="@Model.SeparatedIndustryId" />
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>总部</label>
                                    <div class="input-group">
                                        <input type="hidden" id="hidParentShopId" value="@Model.ParentShopId" />
                                        <select id="ParentShopId" name="ParentShopId" class="form-control" disabled="disabled">
                                            <option value="0">无总部</option>
                                            @{
                                                var parentShopList = ShopsBLL.Instance.GetModelList("IsHeadquarters=1");
                                                foreach (var item in parentShopList)
                                                {
                                                    <option value="@item.Id">@item.Name</option>
                                                }
                                            }
                                        </select>
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>

                                <div class="form-group col-md-6">
                                    <label>描述</label>
                                    <div class="input-group">
                                        @Html.TextAreaFor(model => model.Description, new { @class = "form-control", rows = "3", placeholder = "描述" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div id="secSetCloseTime" class="form-group col-md-6" style="display: none">
                                    <label>打烊时间</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.CloseTimeStart, new { @class = "form-control form_datetime", placeholder = "开始" })
                                        @Html.TextBoxFor(model => model.CloseTimeEnd, new { @class = "form-control form_datetime", placeholder = "结束" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-12">
                                    <label>地址</label>
                                    <div>
                                        当前已存储的地址： @Html.DisplayFor(model => model.Address, new { @class = "form-control" })
                                    </div>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Address, new { @class = "form-control", placeholder = "地址" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                    <br />
                                    <div style="border: 1px solid gray; min-height: 200px" id="mapContainer"></div>
                                    <a class="btn btn-primary" id="btnclear">清除选择</a>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>经度</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Longitude, new { @class = "form-control", @readOnly = "readOnly", placeholder = "经度" })
                                        <span class="input-group-addon">°E</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>纬度</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Latitude, new { @class = "form-control", @readOnly = "readOnly", placeholder = "纬度" })
                                        <span class="input-group-addon">°N</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>法人</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.LegalPerson, new { @class = "form-control", placeholder = "法人" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>法人联系电话</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.LegalPersonMobile, new { @class = "form-control", placeholder = "法人联系电话" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>邮编</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.PostNumber, new { @class = "form-control", placeholder = "邮编" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>片区</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.SalesTerritory, new { @class = "form-control", placeholder = "片区" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>市场</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.MarketAreaName, new { @class = "form-control", placeholder = "市场" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>所属经销商</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.BelongDistributor, new { @class = "form-control", placeholder = "所属经销商" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>电话</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Tel, new { @class = "form-control", placeholder = "电话" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>营业执照编码</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.BusinessLicenseNo, new { @class = "form-control", placeholder = "营业执照编码" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <input id="fileImage" class="form-control" type="file" name="fileImage" style="display: none;" />
                                    <input id="btnSelect" type=button class="btn btn-info" value="上传Logo" onclick="fileImage.click()" />
                                    <img id="imgThumb" alt="缩略图" style="max-height: 80px" src="/Content/admin/image/uploadimg.png" />
                                    <span>（200KB以内）</span>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>销量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.SalesVolume, new { @class = "form-control", @readonly = "readonly", placeholder = "销量" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-offset-6 col-md-6">
                                    <label>
                                        是否为总部
                                        @Html.CheckBoxFor(model => model.IsHeadquarters, new { disabled = "disabled" })
                                    </label>
                                </div>
                                <div class="form-group col-md-offset-6 col-md-6">
                                    <label>
                                        前台是否可见
                                        @Html.CheckBoxFor(model => model.CanFrontView)
                                    </label>
                                </div>
                                <div class="form-group col-md-offset-6 col-md-6">
                                    <label>
                                        开启抢单设置
                                        @Html.CheckBoxFor(model => model.IsOpenOrderGrab)
                                    </label>
                                </div>
                                <div class="form-group col-md-offset-6 col-md-6">
                                    <label>
                                        开启管理后台账户
                                        @Html.CheckBoxFor(model => model.Enabled)
                                    </label>
                                </div>
                                <div class="form-group col-md-offset-6 col-md-6">
                                    <label>
                                        是否可发券
                                        @Html.CheckBoxFor(model => model.CanSendCard)
                                    </label>
                                </div>
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-primary">确认并保存</button>
                                    <a id="btnCheckAccount" class="btn btn-openid" style="display: none">查看账号</a>
                                    <a id="btnCreateAccount" class="btn btn-openid" style="display: none">创建后台账号</a>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

@section javascript{
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=0lypPbMMUsMDyrLk2G1meqGD"> </script>
    <script src="/Content/admin/dist/js/baiduMapHelper.js"> </script>
    <script src="/Content/admin/dist/js/imageUploader.js"> </script>
    <script type="text/javascript">
        // Baidu Map
        localBaiduMap.generate(Longitude, Latitude, Address, mapContainer, btnclear);
        // Image Upload
        imageUploader.uploadSingle(fileImage, btnSelect, imgThumb, '@ViewBag.theLogo');

        var accId = '@ViewBag.SuperAccountId';

        $(function() {
            SetCheckCreateAccountButton();

            if ($("#CanFrontView").prop("checked") == true) {
                $("#CanFrontView").attr("disabled", "disabled");
            }

            var businessStatus = $('option:selected', $('#BusinessStatus')).val();
            if (businessStatus == 1) {
                $("#secSetCloseTime").hide();
            } else {
                $("#secSetCloseTime").show();
            }

            $("#ParentShopId").val($("#hidParentShopId").val());

            $("#frmUpdate").validate({
                rules: {
                    Name: {
                        required: true,
                        remote: {
                            url: "@Url.Action("CheckShopName")",
                            dataType: "json",
                            type: "post",
                            data: {
                                id: @(Model.Id)
                            }
                        }
                    },
                    LegalPerson: { required: true },
                    LegalPersonMobile: { required: true, number: true },
                    PostNumber: { number: true }
                },
                messages: {
                    Name: { required: "请填写店铺名称", remote: "此店铺名已存在" },
                    LegalPerson: { required: "请填写法人姓名" },
                    LegalPersonMobile: { required: "请填写法人姓名" }
                },
                submitHandler: function (form) {
                    var finalBStatus = $('option:selected', $('#BusinessStatus')).val();
                    if (finalBStatus == 2) {
                        var closeStart = $("#CloseTimeStart").val();
                        var closeEnd = $("#CloseTimeEnd").val();
                        if (closeStart > closeEnd) {
                            alertify.error("结束时间需要晚于开始时间");
                            return false;
                        }
                    }

                    form.submit();
                }
            });

            $("#Enabled").change(function() {
                SetCheckCreateAccountButton();
            });

            $("#BusinessStatus").change(function() {
                if ($(this).val() == 1) {
                    $("#secSetCloseTime").hide();
                } else {
                    $("#secSetCloseTime").show();
                }
            });

            $("#btnCreateAccount").click(function() {
                document.location.href = '/McAccounts/Create/' + "@Model.Id";
            });
            $("#btnCheckAccount").click(function() {
                document.location.href = '/McAccounts/Update/' + accId;
            });

            getSeparatedIndustry();
            $("#IndustryId").change(function() {
                getSeparatedIndustry();
            });
        });

        function SetCheckCreateAccountButton() {
            if ($("#Enabled").is(":checked")) {
                if (accId == '0') {
                    $("#btnCheckAccount").hide();
                    $("#btnCreateAccount").show();
                } else {
                    $("#btnCheckAccount").show();
                    $("#btnCreateAccount").hide();
                }
            } else {
                $("#btnCheckAccount").hide();
                $("#btnCreateAccount").hide();
            }
        }

        function getSeparatedIndustry() {
            $("#SeparatedIndustryId").html("");
            $.post("@(Url.Action("GetIndustryByParentId", "Industry"))", "parentId=" + $("#IndustryId").val(), function (data) {
                var separatedIndustryId = $("#hidSeparatedIndustryId").val();
                for (var i = 0; i < data.length; i++) {
                    var sel = data[i].Id == separatedIndustryId ? "selected='selected'" : "";
                    $("#SeparatedIndustryId").append("<option value='" + data[i].Id + "' " + sel + ">" + data[i].Name + "</option>");
                }
            });
        }
    </script>
}